<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="mycanvas" width="500" height="500"></canvas>
</body>
<script>
    // 加载图片 canvas  绘制出来

    let canvas = document.querySelector("#mycanvas");
    let ctx = canvas.getContext("2d");
    // let img = new Image();
    // img.src = 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3355464299,584008140&fm=26&gp=0.jpg';
    // img.onload = function(){
    //     ctx.drawImage(img, 0, 0);
    // }

    function loadImg(src) {
        return new Promise((res, rej) => {
            let img = new Image();
            img.src = src;
            img.onload = function () {
                res(img);
            }
            img.onerror = function () {
                rej("err");
            }
        })
    }
    let src = 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3355464299,584008140&fm=26&gp=0.jpg';
    // loadImg(src, function (img) {
    //     ctx.drawImage(img, 0, 0);
    // })
    // loadImg(src).then(img=>{
    //     ctx.drawImage(img, 0, 0);
    // })
    async function drawImage(src) {
        try {
            let img = await loadImg(src)
            ctx.drawImage(img, 0, 0)
        } catch (error) {
            console.log(error)
        }
    }
    drawImage(src)




</script>

</html>